import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';

// preferences 工具
import preferencesUtil from '../common/utils/PreferencesUtil'

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    // 创建首选项实例
    preferencesUtil.createPreferences(this.context)

  }

  onDestroy() {
  }

  onWindowStageCreate(windowStage: window.WindowStage) {

    // 获取首选项token
    preferencesUtil.getKeyValue('token').then(value=>{
      const token=value
      // 要去的页面：如果有token则进入首页，无则登录
      let goPage='pages/Login'
      if(token){
        goPage='pages/Index'
      }
      windowStage.loadContent('pages/Login', (err, data) => {
        if (err.code) {
          hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
          return;
        }
        hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
      });
    })
  }

  onWindowStageDestroy() {
  }

  onForeground() {
  }

  onBackground() {
  }
}
